package cn.codeforfun.controller;

import cn.codeforfun.util.UserService;
import cn.codeforfun.ws.WSChatService;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Package cn.codeforfun.controller
 * Project cloud-websocket
 * Author WangBin
 * Email bin.wang12@pactera.com
 * Created on 2017/8/14 0014 16:12
 * version 1.0.0
 */
@RestController
public class IndexController {
  private final Logger log = Logger.getLogger(getClass());
  @Resource
  private WSChatService wsChatService;
  @Resource
  private UserService   userService;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public ModelAndView index() {
    ModelAndView mv = new ModelAndView("index");
    return mv;
  }

  @RequestMapping(value = "/send", method = RequestMethod.POST)
  public boolean send(@RequestBody Map map) {
    wsChatService.sendMessage(map);
    return true;
  }

  @RequestMapping(value = "/login/{name}", method = RequestMethod.GET)
  public boolean login(@PathVariable String name) {
    return userService.login(name);
  }

  @RequestMapping(value = "/logout", method = RequestMethod.GET)
  public boolean login() {
    return userService.logout();
  }

  @RequestMapping(value = "/getUser", method = RequestMethod.GET)
  public String getUser(HttpSession session) {
    Object obj = session.getAttribute("user");
    if (obj == null || StringUtils.isEmpty(obj)) return null;
    return (String) obj;
  }

}
